<template>
  <el-dialog
    title="账户充值"
    :visible.sync="dialogVisible"
    width="300px">
    <div>
      <div>当前账户余额: <span class="red b" v-text="'￥'+ToMoney(currMoney)"></span></div>
      <div class="pay-choose-box">
        <div class="title">充值金额</div>
        <div class="item" style="border: 0px;">
          <div class="left">
            <el-select v-model="money" placeholder="充值金额">
              <el-option
                v-for="(money,index) in moneyArr"
                :key="index"
                :label="money"
                :value="money">
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="title">支付方式</div>
        <div class="item" @click="payType=1">
          <div class="left">
            <div class="icon wechat-pay"></div>
            <span>微信</span>
          </div>
          <div class="right">
            <div class="pay-choose-icon" :class="payType===1?'on':''"></div>
          </div>
        </div>
        <div class="item" @click="payType=2">
          <div class="left">
            <div class="icon ali-pay"></div>
            <span>支付宝</span>
          </div>
          <div class="right">
            <div class="pay-choose-icon" :class="payType===2?'on':''"></div>
          </div>
        </div>
      </div>

    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirm">确认支付</el-button>
    </div>
  </el-dialog>

</template>

<script>
export default {
  name: 'MemberRechargeDialog',
  data: function () {
    return {
      callback:null,
      dialogVisible:false,
      money:0.02,
      moneyArr:[0.02,100,200,500,1000,2000,5000],
      payType:1,
      currMoney:0,
    }
  },
  mounted () {
    let $this = this
    $this.bus.$on('BackKeyCallBus',function (){
      $this.dialogVisible=false
      $this.$forceUpdate()
    })
    $this.bus.$on('MemberRechargeDialogCallBus', function (callback) {
      $this.dialogVisible = true
      $this.callback=callback
      $this.getCurrMoney()
      $this.$forceUpdate()
    })

  },
  methods: {
    // 确认充值
    confirm:function () {
      let $this = this
      $this.MyRequest($this.ApiDoNameMember + 'Member/recharge', {
        money:$this.money,
        payType:$this.payType,
      }, function (data) {


        if ($this.payType===1){
          $this.pay('wxpay', {
            appid: data.appid,
            prepayid: data.prepayid,
            partnerid: data.partnerid,
            noncestr: data.noncestr,
            timestamp: data.timestamp,
            package: data.package,
            sign: data.sign
          },function () {
            $this.$message.success('支付成功')
            if (typeof $this.callback === 'function') {
              $this.callback()
            }
            $this.dialogVisible=false
            $this.$forceUpdate()
          },function () {
            $this.$message.info('未完成支付')
          })
          // 微信支付
          // var wxPayPlus = api.require('wxPayPlus')
          // wxPayPlus.payOrder({
          //   apiKey: data.appid,
          //   orderId: data.prepayid,
          //   mchId: data.partnerid,
          //   nonceStr: data.noncestr,
          //   timeStamp: data.timestamp,
          //   package: data.package,
          //   sign: data.sign
          // }, function (ret, err) {
          //   if (ret.status) {
          //     $this.$message.success('支付成功')
          //   } else {
          //     $this.$message.info('充值失败')
          //   }
          // })
          //
          // if (typeof $this.callback==='function'){
          //   $this.callback()
          // }
        }

        if ($this.payType===2){
          $this.pay('alipay', data,function () {
            $this.$message.success('支付成功')
            if (typeof $this.callback === 'function') {
              $this.callback()
            }
            $this.dialogVisible=false
            $this.$forceUpdate()
          },function () {
            $this.$message.info('未完成支付')
          })

          // // 支付宝支付
          // var aliPayPlus = api.require('aliPayPlus');
          // aliPayPlus.payOrder({
          //   orderInfo: data
          // }, function(ret, err) {
          //   $this.$confirm('您是否支付完成?', '系统提示', {
          //     confirmButtonText: '已经支付',
          //     cancelButtonText: '返回订单',
          //     type: 'warning'
          //   }).then(() => {
          //     $this.$message.success('充值成功')
          //     if (typeof $this.callback==='function'){
          //       $this.callback()
          //     }
          //   }).catch(() => {
          //     $this.$message.info('充值失败')
          //   })
          // });
        }


        $this.$forceUpdate()
      }, function (msg) {
        $this.$message.error(msg)
      })

    },
    // 获取当前账户余额
    getCurrMoney:function () {
      let $this = this
      $this.MyRequest($this.ApiDoNameMember + 'Member/info', {}, function (data) {
        $this.currMoney = data.money
        $this.$forceUpdate()
      }, function (msg) {
        $this.$message.error(msg)
      })
    }
  }
}
</script>

<style scoped>

</style>
